﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022 北京超维景生物科技有限公司 保留所有权利。
 * 
 * 创建者：huangyang
 * 电子邮箱：huangyang@tvscope.cn
 * 创建时间：2023/2/16 11:51:57
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageK.Process;

namespace ImageK.Plugin.Frame
{
    public class ContrastPlot:Panel
    {
        const int WIDTH = 128, HEIGHT = 64;
        public double defaultMin = 0;
        public double defaultMax = 255;
        public double min = 0;
        public double max = 255;
        int[] histogram;
        int hmax;
        Image os;
        Graphics osg;
        Color color = Color.Gray;
        double scale = Prefs.getGuiScale();
        int _width = WIDTH;
        int _height = HEIGHT;

        public ContrastPlot()
        {
            // addMouseListener(this);
            if (scale > 1.0)
            {
                _width = (int)(_width * scale);
                _height = (int)(_height * scale);
            }

            this.Width = _width + 1;
            this.Height = _height + 1;
        }

        public void setHistogram(ImageStatistics stats, Color color)
        {
            this.color = color;
            histogram = stats.Histogram;
            if (histogram.Length != 256)
            {
                histogram = null;
                return;
            }
            int maxCount = 0;
            int mode = 0;
            for (int i = 0; i < 256; i++)
            {
                if (histogram[i] > maxCount)
                {
                    maxCount = histogram[i];
                    mode = i;
                }
            }
            int maxCount2 = 0;
            for (int i = 0; i < 256; i++)
            {
                if ((histogram[i] > maxCount2) && (i != mode))
                    maxCount2 = histogram[i];
            }
            hmax = stats.maxCount;
            if ((hmax > (maxCount2 * 2)) && (maxCount2 != 0))
            {
                hmax = (int)(maxCount2 * 1.5);
                histogram[mode] = hmax;
            }
            os = null;
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            base.OnPaint(e);
            int x1, y1, x2, y2;
            double scale = (double)_width / (defaultMax - defaultMin);
            double slope = 0.0;
            if (max != min)
                slope = _height / (max - min);
            if (min >= defaultMin)
            {
                x1 = (int)(scale * (min - defaultMin));
                y1 = _height;
            }
            else
            {
                x1 = 0;
                if (max > min)
                    y1 = _height - (int)((defaultMin - min) * slope);
                else
                    y1 = _height;
            }
            if (max <= defaultMax)
            {
                x2 = (int)(scale * (max - defaultMin));
                y2 = 0;
            }
            else
            {
                x2 = _width;
                if (max > min)
                    y2 = _height - (int)((defaultMax - min) * slope);
                else
                    y2 = 0;
            }
            if (histogram != null)
            {
                if (os == null && hmax != 0)
                {
                    os = new Bitmap(_width, _height);
                    osg = Graphics.FromImage(os);
                    osg.FillRectangle(new SolidBrush(Color.White),0,0,_width,_height);
                    var pen = new Pen(color);
                    double scale2 = _width / 256.0;
                    for (int i = 0; i < 256; i++)
                    {
                        int x = (int)(i * scale2);
                        osg.DrawLine(pen, x, _height, x, _height - ((int)(_height * histogram[i]) / hmax));
                    }
                    osg.Dispose();
                }
                if (os != null) g.DrawImage(os, 0, 0);
            }
            else
            {
                g.FillRectangle(new SolidBrush(Color.White), 0, 0, _width, _height);
            }

            var bgPen = new Pen(Color.Black);
            g.DrawLine(bgPen, x1, y1, x2, y2);
            g.DrawLine(bgPen, x2, _height - 5, x2, _height);
            g.DrawRectangle(bgPen, 0, 0, _width, _height);
        }
    }
}
